package com.app.controller.authority;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.app.controller.BaseController;
import com.app.entity.Menu;
import com.app.entity.authority.User;
import com.app.service.authority.MenuService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/menu")
public class MenuController extends BaseController {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    //selectMenuById

    @RequestMapping(value = "/getInfoById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> selectMenuById(@RequestBody Menu m) {
        Map<String, Object> results = new HashMap<>();
        try {
            List<Menu> menu = menuService.getInfoById(m.getId());
            if (menu != null && menu.size()>0) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", menu.get(0));
            }else if(menu != null && menu.size()==0){
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", new Menu());
            }


            else {
                results.put("code", 201);
                results.put("message", "操作失败！");
                results.put("data", null);
            }
        } catch (Exception e) {
            results.put("code", 500);
            results.put("message", e);
        }
        return results;
    }

    ///lj add

    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)//value属性：映射的是请求的地址;method属性：定义了请求的方式
    @ResponseBody//将controller的方法返回的对象转换为指定的格式之后，写入到response对象的body区，通常用来返回JSON数据或者是XML
    public Map<String, Object> getInfo(@RequestBody Menu m) {
        Map<String, Object> results = new HashMap<>();
        try {
            List<Menu> menu = menuService.getInfo(m.getId());
            if (menu != null) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", menu);
            } else {
                results.put("code", 201);
                results.put("message", "操作失败！");
                results.put("data", null);
            }
        } catch (Exception e) {
            results.put("code", 500);
            results.put("message", e);
        }
        return results;
    }

    @RequestMapping(value="/getInfoByParentId",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMenuByParentId(@RequestBody Menu _menu){

        Map<String, Object> result=new HashMap<String, Object>();
        try {
            List<Menu> menus=this.menuService.queryMenuByParentId(_menu.getParentId());
            if (menus!=null) {
                result.put("code",200);
                result.put("message", "success");
                result.put("data", menus);
            }
            else {
                result.put("code", 201);
                result.put("message", "notexists");
            }

            return result;
        } catch (Exception e) {
            log.error("func:queryMenuByParentId %_menu:"+_menu+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }

    }

    @RequestMapping(value="/delInfo",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delDepart(@RequestBody Menu _menu) {
        Map<String, Object> results = new HashMap<String, Object>();

        if (_menu.getId() == null) {
            results.put("code", 400);
            results.put("message", "操作失败!请联系管理员！");
            results.put("data", "-1");
            return results;
        }

        try {

            List<Menu> list = this.menuService.queryMenuByParentId(_menu.getId());

            if (list.size() < 1) {
                Integer resultcount = this.menuService.delMenu(_menu);
                if (resultcount >= 0) {
                    results.put("code", 200);
                    results.put("message", "操作成功！");
                    results.put("data", _menu);
                    return results;
                } else {
                    results.put("code", 201);
                    results.put("message", "操作失败!请联系管理员！");
                    results.put("data", "-1");
                    return results;
                }
            }else{
                results.put("code",201);
                results.put("message", "存在下级部门，不能删除！");
                results.put("data",_menu);
            }
            return results;
        } catch (Exception e) {
            log.error("func:updateMenu %n_menu:" + _menu + "%n" + e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }


    /**
     * @param
     * @return
     */
    @RequestMapping(value="/modifyInfo",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateMenu(@RequestBody Menu menu){

        if(menu.getParentId() == null || menu.getParentId().length()<=0){
            menu.setParentId("0");
        }

        Map<String, Object> results=new HashMap<String, Object>();
        try {
            Integer resultcount=this.menuService.updateMenu(menu);

            if (resultcount>=0) {
                results.put("code",200);
                results.put("message", "操作成功！");
                results.put("data",menu);
                return results;
            }
            else {
                results.put("code",201);
                results.put("message", "操作失败!请联系管理员.");
                results.put("data","-1");
                return results;
            }

        } catch (Exception e) {
            log.error("func:updateMenu %n_menu:"+menu+"%n"+e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }

    /**
     * @param
     */
    @RequestMapping(value="/addInfo",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertMenu(@RequestBody Menu _menu){
        _menu.setId(java.util.UUID.randomUUID().toString());

        if(_menu.getParentId() == null || _menu.getParentId().length()<=0){
            _menu.setParentId("0");
        }

        Map<String, Object> results=new HashMap<String, Object>();
        try {
            Integer resultcount=this.menuService.insertMenu(_menu);
            if (resultcount>0) {
                results.put("code",200);
                results.put("message", "操作成功！");
                results.put("data",_menu);
                return results;
            }
            else {
                results.put("code",201);
                results.put("message", "操作失败!请联系管理员");
                results.put("data","-1");
                return results;
            }

        } catch (Exception e) {
            log.error("func:insertMenu %n_menu:"+_menu+"%n"+e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }

    @RequestMapping(value="/getSecurityInfoAll",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> querySecurityMenu(@RequestBody String  userId){

        Map<String, Object> result=new HashMap<String, Object>();
        try {

            List<Menu> list=this.menuService.querySecurityMenuAll(userId);
            result.put("code",200);
            result.put("message", "succ");
            result.put("data", list);

            return result;
        } catch (Exception e) {
            log.error("func:queryMenu %parentId:"+userId+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }

    }
    @RequestMapping(value="/getInfoAll",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMenu(@RequestBody Menu  menu){

        Map<String, Object> result=new HashMap<String, Object>();
        try {

            List<Menu> list=this.menuService.queryMenuAll(menu.getId());
            result.put("code",200);
            result.put("message", "succ");
            result.put("data", list);

            return result;
        } catch (Exception e) {
            log.error("func:queryMenu %parentId:"+menu.getId()+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }

    }

    @RequestMapping(value="/validateCode",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> validateCode(@RequestBody Menu _menu){
        Map<String, Object> result=new HashMap<String, Object>();
        try {List<Menu> list=this.menuService.validateLevelCode(_menu);

            if(list.size()>0){
                result.put("code",201);
                result.put("message", "编码存在！请选择其他编码");
            }
            else{
                result.put("code",200);
                result.put("message", "succ");
            }


            //	result.put("data", list);

            return result;
        } catch (Exception e) {
            log.error("func:validateCode %_department:"+_menu+"%n"+e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }

    }

//    @RequestMapping(value="/changeForm",method=RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> changeForm(@RequestBody List<PageSetting> pageSettings){
//
//
//        Map<String, Object> results=new HashMap<String, Object>();
//        try {
//            menuService.changeForm(pageSettings);
//            return results;
//        } catch (Exception e) {
//            log.error("func:insertMenu %n_menu:"+pageSettings+"%n"+e.getMessage());
//            results.put("code", 500);
//            results.put("message", e.getMessage());
//            return results;
//        }
//    }

    @RequestMapping(value = "/updateSort", method = RequestMethod.POST)//value属性：映射的是请求的地址;method属性：定义了请求的方式
    @ResponseBody//将controller的方法返回的对象转换为指定的格式之后，写入到response对象的body区，通常用来返回JSON数据或者是XML
    public Map<String, Object> updateSort(@RequestBody  Menu menu) {
        Map<String, Object> results = new HashMap<>();
        try {

            menuService.updateSort(menu);
            results.put("code",200);
            results.put("message", "succ");

        } catch (Exception e) {
            results.put("code", 500);
            results.put("message", e);
        }
        return results;
    }
}